package com.zxmzy.scaffold.core.base

import android.os.Bundle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.zxmzy.scaffold.core.ext.saveAs
import com.zxmzy.scaffold.core.loading.LoadingManager
import java.lang.reflect.ParameterizedType

/**
 * @author zhangxiaoming
 * @date 2023/7/25
 * @description DataBinding+ViewModel基类
 */
abstract class BaseMvvmActivity<VB : ViewBinding, VM : ViewModel> : BaseActivity<VB>() {
    protected lateinit var mViewModel: VM
    protected val loadingManager: LoadingManager by lazy {
        LoadingManager(this);
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        initViewModel()
        super.onCreate(savedInstanceState)
    }

    private fun initViewModel() {
        val type = this.javaClass.genericSuperclass
        val argument = type!!.saveAs<ParameterizedType>().actualTypeArguments
        mViewModel = ViewModelProvider(this).get(argument[1].saveAs())

    }
}